package com.ikas.ai.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.MemoryCacheImageInputStream;
import java.io.*;
import java.util.Iterator;

/**
 * 根据图片路径获得图片类型.
 * @author 黄旭辉
 */
@SuppressWarnings("all")
public class ImageTypeUtil {

    private static Logger logger = LoggerFactory.getLogger(ImageTypeUtil.class);

    /**
     * @param args 参数
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        /*String rtn = null;
        rtn = getImageType("H:\\图形\\壁纸\\1.gif");
        logger.debug(rtn);
        rtn = getImageType("H:\\图形\\壁纸\\test.gif");
        logger.debug(rtn);
        rtn = getImageType("H:\\图形\\壁纸\\test.jpg");
        logger.debug(rtn);
        rtn = getImageType("H:\\图形\\壁纸\\test.png");
        logger.debug(rtn);
        rtn = getImageType("H:\\图形\\壁纸\\test.bmp");
        logger.debug(rtn);*/

    }

    /**
     * 根据图片路径获得图片类型.
     * @param path
     * @return
     * @throws IOException
     */
    public static String getImageType(String path) throws IOException {
        FileInputStream fis = new FileInputStream(path);
        return getImageType(fis);
    }

    /**
     * 根据图片文件流获得图片类型.
     * @param input
     * @return
     * @throws IOException
     */
    public static String getImageType(InputStream input) throws IOException {
        int leng = input.available();
        BufferedInputStream buff = new BufferedInputStream(input);
        byte[] mapObj = new byte[leng];
        buff.read(mapObj, 0, leng);
        buff.close();
        return getImageType(mapObj);
    }

    /**
     * 根据图片字节获得图片类型.
     * @param imageBytes
     * @return
     * @throws IOException
     */
    public static String getImageType(byte[] imageBytes) throws IOException {
        String type = "";
        ByteArrayInputStream bais = null;
        MemoryCacheImageInputStream mcis = null;
        try {
            bais = new ByteArrayInputStream(imageBytes);
            mcis = new MemoryCacheImageInputStream(bais);
            Iterator<ImageReader> itr = ImageIO.getImageReaders(mcis);
            while (itr.hasNext()) {
                ImageReader reader = (ImageReader) itr.next();
                type = reader.getFormatName().toLowerCase();
            }
        } finally {
            closeInputStream(bais);
            closeInputStream(mcis);
        }
        return type;
    }


    /**
     * 关闭文件流
     * @param in
     */
    private static void closeInputStream(Object in) {
        if (in != null) {
            try {
                if (in instanceof InputStream) {
                    ((InputStream) in).close();
                } else if (in instanceof MemoryCacheImageInputStream) {
                    ((MemoryCacheImageInputStream) in).close();
                }
            } catch (IOException e) {
                logger.error("InputStream close failure. ", e);
            }
        }
    }
}
